/**
 * @file Hyper.c
 *
 * Funciones del Hiperterminal
 *
 * @version  1.00a
 * @date     26/6/2014
 * @author   Pirx
 *
 */

/***************************** Include Files **********************************/

#include <stdio.h>
#include <string.h>
#include "HyperEva.h"
#include "HardEva.h"
#include "UART_lib.h"
#include "Lm95071.h"


/************************** Constant Definitions ******************************/

#define	HYPER_TIME_SCAN		500		// Teimpo de Escan  en milisegundos


/**************************** Type Definitions ********************************/

typedef struct{
	BOOL		_scan;				// Scan Activo = TRUE
	BOOL		_gateway;			// GateWay Activo = TRUE
	BOOL		_aob;				// Seleccion de la cadena de Entrada
	BOOL		_erin;				// Error de Entrada
	UINT16		tmrScan;			// Timer Entrada de Datos Valida
	UINT16		tmrGate;			// Timer GateWay Activo
	UINT8		stringA[6];			// Cadena de Entrada
	UINT8		stringB[12];		// Cadena de Entrada
	UINT8		Line[32];			// Linea Formateada de Salida
	UINT8		ptrA;				// Puntero Cadena A
	UINT8		ptrB;				// Puntero Cadena B
	UINT8		sTrx;				// String en transmision
	UINT8		sEnd;				// String final en transmision
}StrHyper;


/***************************** Variable Definitions ***************************/

const char *HyperData[]={
//	 --------------------------------
	"OK                          \n\r\n\r",		// 0 OK
	"\n\rError <Type Help+Enter> \n\r\n\r",		// 1 Error
	"\n\rInfo+Enter <Device Info>    \n\r",		// 2 Help
	"Mode=[ON/Sleep] <Set Mode>    \n\r",
	"Scan=[On] <Set Scan>(ESC=quit)\n\r",
	"Gate=[On] <Set Gateway ON>  \n\r\n\r",
//	 --------------------------------
};

static StrHyper Modem;


/************************** Function Prototypes *******************************/

static BOOL	HyperProc(UINT8 d);					// Procesa los datos recibidos
static BOOL	HyperCommand(void);					// Procesa la Entrade de Datos
static BOOL	HyperPrep(UINT8 lin);				// Prepara Linea de Salida
static void HyperResetInp(void);				// Inicializar Cadena de Entrada
static void HyperScan(void);					// Monitorizacion de los Datos


/******************************************************************************/
/**
* Inicializacion
*
* @param	None
* @return 	None
* @note		None
*
******************************************************************************/
extern void HyperInit(void)
{
	Modem._scan = FALSE;
	Modem._gateway = FALSE;
	Modem.tmrScan = (UINT16)((1000*(UINT32)HYPER_TIME_SCAN)/(UINT32)TIMEBASE);
	Modem.tmrGate = 0;
	Modem._erin = FALSE;
	Modem._aob = FALSE;
	Modem.ptrA = 0;
	Modem.ptrB = 0;
	Modem.sTrx = 0;
	Modem.sEnd = 0;
}


/******************************************************************************/
/**
* Control de Timers
*
* @param	None
* @return 	None
* @note		None
*
******************************************************************************/
extern BOOL HyperTimer(void)
{
	BOOL _ret = FALSE;

	// Timer de Transmision Periodica de los Datos
	if (Modem.tmrScan > 0){
		Modem.tmrScan--;
		if (Modem.tmrScan == 0){
			HyperScan();
			Modem.tmrScan = (UINT16)((1000*(UINT32)HYPER_TIME_SCAN)/(UINT32)TIMEBASE);
			_ret = TRUE;
		}
	}
	
	return (_ret)? TRUE : FALSE;
}


/******************************************************************************/
/**
* Control Principal del Hyperterminal
*
* @param	dev		Numero de Dispositivo
* @return 	TRUE = Byte transmitido
* @note		None
*
******************************************************************************/
extern BOOL HyperMain(void)
{
	BOOL _ret = FALSE;	

	// Procesar Datos Recibidos
	/*while (UART_DataAvailable(UART_1) == TRUE){
		UINT8 d = UART_GetData(UART_1);
		HyperProc(d);
	}

	// Transmitir Datos Pendientes
	if (Modem.sTrx != Modem.sEnd){
		HyperPrep(Modem.sTrx);
		UART_Transmit(UART_1, Modem.Line, sizeof(Modem.Line));
		Modem.sTrx++;
	}*/
	
	return (_ret)? TRUE : FALSE;
}


/******************************************************************************/
/**
* Procesa los Datos Recibidos
*
* @param	Nuevo Dato
* @return 	Always TRUE
* @note		None
*
******************************************************************************/
static BOOL	HyperProc(UINT8 d)
{
	BOOL _ret = FALSE;

	// Eco del Hyperterminal
	UART_Transmit(UART_1, &d, 1);

	return (_ret)? TRUE : FALSE;
}


/******************************************************************************/
/**
* Procesa la Entrada de Datos
*
* @param	None
* @return 	TRUE = Comando procesado con exito
* @note		None
*
******************************************************************************/
static BOOL	HyperCommand(void)
{
	/*BOOL _ret = FALSE;

	if ((Modem._erin == FALSE) && (Modem.ptrA == 4)){
		switch (Modem.stringA[0]){
			case 'H':
				if ( (Modem.ptrB == 0) &&
					 (Modem.stringA[1] == 'E') &&	
					 (Modem.stringA[2] == 'L') &&	
					 (Modem.stringA[3] == 'P') ){
					Modem.sTrx = 2;
					Modem.sEnd = 6;
				} else {
					Modem._erin = TRUE;
				}
				break;
			case 'I':
				if ( (Modem.ptrB == 0) &&
					 (Modem.stringA[1] == 'N') &&	
					 (Modem.stringA[2] == 'F') &&	
					 (Modem.stringA[3] == 'O') ){
					// Informacion del Sistema
					UART_Transmit(UART_USB, (UINT8*)"\n\rEOIT SmartHorse\n\r", 19);	
				} else {
					Modem._erin = TRUE;
				}
				break;
			case 'M':
				break;
			case 'S':
				if ( (Modem.ptrB == 2) &&
					 (Modem.stringA[1] == 'C') &&	
					 (Modem.stringA[2] == 'A') &&	
					 (Modem.stringA[3] == 'N') &&	
					 (Modem.stringB[0] == 'O') &&	
					 (Modem.stringB[1] == 'N') ){
					Modem._scan = TRUE;
					Modem.tmrScan = (UINT16)((1000*(UINT32)HYPER_TIME_SCAN)/(UINT32)TIMEBASE);
				} else {
					Modem._erin = TRUE;
				}
				break;
			case 'G':
				if ( (Modem.ptrB == 2) &&
					 (Modem.stringA[1] == 'A') &&	
					 (Modem.stringA[2] == 'T') &&	
					 (Modem.stringA[3] == 'E') &&	
					 (Modem.stringB[0] == 'O') &&	
					 (Modem.stringB[1] == 'N') ){
					if (Modem._scan == TRUE){
						UART_Transmit(UART_USB, (UINT8*)"\n\rMode Scan = OFF\n\r", 19);	
						Modem._scan = FALSE;
						Modem.tmrScan = 0;
					}
					HyperKeepGate();
					UART_Transmit(UART_USB, (UINT8*)"\n\rGateway Active\n\r", 18);	
				} else {
					Modem._erin = TRUE;
				}
				break;
			default:
				Modem._erin = TRUE;
				break;
		}
	} else {
		Modem._erin = TRUE;
	}

	if (Modem._erin == TRUE){
		Modem.sTrx = 1;
		Modem.sEnd = 2;
	}

	HyperResetInp();					// Resetear Cadena de Entrada

	return (_ret)? TRUE : FALSE;*/
}


/******************************************************************************/
/**
* Prepara linea de Salida
*
* @param	Numero de Linea
* @return 	TRUE = Linea Valida
* @note		None
*
******************************************************************************/
static BOOL	HyperPrep(UINT8 lin)
{
	BOOL _ret = TRUE;

	// Copiar Linea en el Buffer
	memcpy(Modem.Line, HyperData[lin], sizeof(Modem.Line));

	// Rellenar
	switch (lin){
		case 0:
			//
			break;
		default:
			_ret = FALSE;
			break;
	}

	return (_ret)? TRUE : FALSE;
}


/******************************************************************************/
/**
* Inicializacion de la Cadena de Entrada
*
* @param	None
* @return 	None
* @note		None
*
******************************************************************************/
static void HyperResetInp(void)
{
	// Resetear Cadena de Entrada
	Modem.ptrA = 0;
	Modem.ptrB = 0;
	Modem._aob = FALSE;
	Modem._erin = FALSE;
}


/******************************************************************************/
/**
* Monitorizacion USB (transmision de datos)
*
* @param	None
* @return 	None
* @note		None
*
******************************************************************************/
static void HyperScan(void)
{
	static StrBCD*	texto;

	// Cabecera
	UART_Transmit(UART_1, (UINT8*)"\n\r", 2);
	UART_Transmit(UART_1, (UINT8*)"\n\r", 2);
	UART_Transmit(UART_1, (UINT8*)"\n\r", 2);
	UART_Transmit(UART_1, (UINT8*)"Si85xxEva (Monitor de Intensidad)\n\r", 35);
	UART_Transmit(UART_1, (UINT8*)"---------------------------------\n\r", 35);

	// Temperatura:
	UART_Transmit(UART_1, (UINT8*)"Temperatura:", 12);
	texto = ConvBCD((int)((Lm95071_get_Celsius(0))/10), FALSE, TRUE, FALSE);
	UART_Transmit(UART_1, &texto->data[8], 3);
	UART_Transmit(UART_1, (UINT8*)",", 1);
	UART_Transmit(UART_1, &texto->data[11], 1);
	UART_Transmit(UART_1, (UINT8*)" gC\n\r", 5);

	// Intensidad
	UART_Transmit(UART_1, (UINT8*)"Intensidad :", 12);
	UART_Transmit(UART_1, (UINT8*)" --,-", 5);
	UART_Transmit(UART_1, (UINT8*)"Amp\n\r", 5);
}


/******************************************************************************/
/**
* Obtener el estado del Gateway
*
* @param	None
* @return 	Estado
* @note		None
*
******************************************************************************/
extern BOOL HyperGetGate(void)
{
	return Modem._gateway;
}


/******************************************************************************/
/**
* Transmite un Byte
*
* @param	Byte de Dato
* @return 	None
* @note		None
*
******************************************************************************/
extern void HyperTxByte(UINT8 dt)
{
	/*if (dt == 0xd){								// Avance de linea si Gate = On
		UART_Transmit(UART_USB, (UINT8*)"\n\r", 2);
	} else {
		UART_Transmit(UART_USB, &dt, 1);
	}*/
}
